import { CSLine, CSLineAttrs } from './CSLine';
import { THREE } from '@amcax/web-render';
export type CSCurveAttrs = {
  curvePoints?: THREE.Vector3Tuple[]; // 点
} & Omit<CSLineAttrs, 'vertexs'>;
// 默认点数
const defaultPoints = 25;
export class CSCurve extends CSLine {
  defaultPoints = 25;
  axisCoord: {
    matrix: THREE.Matrix4;
    worldToLocal: THREE.Matrix4;
  };
  constructor(attrs?: CSCurveAttrs) {
    super({
      maxPoints: attrs?.maxPoints ? attrs.maxPoints * defaultPoints : null,
      ...attrs,
    });
    this.initAttrs(attrs);
  }
  initAttrs(attrs?: CSCurveAttrs) {
    this.userData = { ...attrs };
    if (attrs?.curvePoints) {
      this.updateCurve(attrs);
    }
  }
  updateCurve(attrs?: CSCurveAttrs) {
    const vertexs = this.getCirclePoints(attrs.curvePoints);
    this.setVertexs(vertexs.map((v) => v.toArray()));
    this.update(attrs);
  }
  getCirclePoints(points: THREE.Vector3Tuple[]) {
    // 2. 创建 Catmull-Rom 曲线
    const curve = new THREE.CatmullRomCurve3(
      points?.map((p) => new THREE.Vector3(...p)),
      false,
    ); // 第二个参数控制是否闭合
    return curve.getPoints(defaultPoints * (points?.length - 1));
  }
}
